package com.gallsoft.amidar;

import com.gallsoft.amidar.GameObject.ActionType;

public class AnimationComponent extends GameComponent {
	
    public enum PlayerAnimations {
    	IDLE,
        MOVE_HORIZ,
        MOVE_VERT,
        DEATH
    }

    private SpriteComponent mSprite;
    private PlayerComponent mPlayer;

    private GameObject.ActionType mPreviousAction;

    public AnimationComponent() {
        super();
        reset();
        setPhase(ComponentPhases.ANIMATION.ordinal());
    }

    @Override
    public void reset() {
        mSprite = null;
        mPlayer = null;
    }
    
    @Override
    public void update(float timeDelta, BaseObject parent) {
        if (mSprite != null) {

            GameObject parentObject = (GameObject) parent;            
            GameObject.ActionType currentAction = parentObject.getCurrentAction();
            
            if (currentAction == ActionType.MOVE_HORIZ || currentAction == ActionType.MOVE_VERT) {
                InputGameInterface input = sSystemRegistry.inputGameInterface;
                final InputXY dpad = input.getDirectionalPad();
                mSprite.setActive(true);
                
                if (currentAction == ActionType.MOVE_HORIZ) {
	                if (dpad.getX() < 0.0f) {
	                    parentObject.facingDirection.x = 1.0f; 
	                    mSprite.playAnimation(PlayerAnimations.MOVE_HORIZ.ordinal());
	                } 
	                else if (dpad.getX() > 0.0f) {
	                    parentObject.facingDirection.x = -1.0f;
	                    mSprite.playAnimation(PlayerAnimations.MOVE_HORIZ.ordinal());
	                }                
                }

                if (currentAction == ActionType.MOVE_VERT) {
	                if (dpad.getY() < 0.0f) {
	                    parentObject.facingDirection.y = 1.0f; 
	                    mSprite.playAnimation(PlayerAnimations.MOVE_VERT.ordinal());
	                } 
	                else if (dpad.getY() > 0.0f) {
	                    parentObject.facingDirection.y = 1.0f;
	                    mSprite.playAnimation(PlayerAnimations.MOVE_VERT.ordinal());
	                }
                }
            }
            else
            	mSprite.setActive(false);
            
            
            mPreviousAction = currentAction;
        }
    }
    
    public void setPlayer(PlayerComponent player) {
        mPlayer = player;
    }

    public void setSprite(SpriteComponent sprite) {
        mSprite = sprite;
    }

}
